﻿using System;
using System.Data;
using System.Data.Common;
using System.Text;
using Besture.Common;
using Besture.Model;

namespace Besture.Dal
{
    public partial class ReviewsReplyDal : BaseDataAccess
    {

        /// <summary>
        /// 获取详细信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public ReviewsReplyModel GetModel(int id)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBReviewsReply");
                selectSql.Append(" WHERE ");
                selectSql.Append(" ID=@ID ");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                if (dt.Rows.Count > 0)
                    return ModelConvertHelper<ReviewsReplyModel>.ConvertToModelList(dt)[0];
                else return null;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable()
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBReviewsReply ORDER BY ID DESC ");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                return dt;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(ReviewsReplyModel model)
        {
            var identityId = -1;
            try
            {

				
            string sqlString = string.Format(@"
                INSERT INTO TBReviewsReply
                (
                        ReviewsID
                        ,UserID
                        ,ReplyUserID
                        ,ReplyContent
                        ,SortID
                        ,IsDeleted
                        ,Created
                        ,CreateTime
                        ,Updated
                        ,UpdateTime
                )
                VALUES
                (
                        @ReviewsID
                       ,@UserID
                       ,@ReplyUserID
                       ,@ReplyContent
                       ,@SortID
                       ,@IsDeleted
                       ,@Created
                       ,@CreateTime
                       ,@Updated
                       ,@UpdateTime
                )SELECT @@IDENTITY");

            DbCommand ldcDBCommand = base.GetCommand(sqlString);

            //参数设定
            SetParameter(ldcDBCommand, "@ReviewsID", DbType.Int32, model.ReviewsID);
            SetParameter(ldcDBCommand, "@UserID", DbType.Int32, model.UserID);
            SetParameter(ldcDBCommand, "@ReplyUserID", DbType.Int32, model.ReplyUserID);
            SetParameter(ldcDBCommand, "@ReplyContent", DbType.String, model.ReplyContent);
            SetParameter(ldcDBCommand, "@SortID", DbType.Int32, model.SortID);
            SetParameter(ldcDBCommand, "@IsDeleted", DbType.Int32, model.IsDeleted);
            SetParameter(ldcDBCommand, "@Created", DbType.Int32, model.Created);
            SetParameter(ldcDBCommand, "@CreateTime", DbType.DateTime, model.CreateTime);
            SetParameter(ldcDBCommand, "@Updated", DbType.Int32, model.Updated);
            SetParameter(ldcDBCommand, "@UpdateTime", DbType.DateTime, model.UpdateTime);



                identityId = base.ExecuteScalarInsert(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return identityId;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(ReviewsReplyModel model)
        {
            int iTmp = -1;
            try
            {
				
            string sqlString = string.Format(@"
                UPDATE TBReviewsReply
                SET
                    ReviewsID = @ReviewsID
                    ,UserID = @UserID
                    ,ReplyUserID = @ReplyUserID
                    ,ReplyContent = @ReplyContent
                    ,SortID = @SortID
                    ,IsDeleted = @IsDeleted
                    ,Created = @Created
                    ,Updated = @Updated
                    ,UpdateTime = @UpdateTime
                WHERE
                    ID = @ID
            ");

            DbCommand ldcDBCommand = base.GetCommand(sqlString);
            //绑定SQL参数
            SetParameter(ldcDBCommand, "@ID", DbType.Int32, model.ID);
            SetParameter(ldcDBCommand, "@ReviewsID", DbType.Int32, model.ReviewsID);
            SetParameter(ldcDBCommand, "@UserID", DbType.Int32, model.UserID);
            SetParameter(ldcDBCommand, "@ReplyUserID", DbType.Int32, model.ReplyUserID);
            SetParameter(ldcDBCommand, "@ReplyContent", DbType.String, model.ReplyContent);
            SetParameter(ldcDBCommand, "@SortID", DbType.Int32, model.SortID);
            SetParameter(ldcDBCommand, "@IsDeleted", DbType.Int32, model.IsDeleted);
            SetParameter(ldcDBCommand, "@Created", DbType.Int32, model.Created);
            SetParameter(ldcDBCommand, "@Updated", DbType.Int32, model.Updated);
            SetParameter(ldcDBCommand, "@UpdateTime", DbType.DateTime, model.UpdateTime);


                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("DELETE FROM TBReviewsReply");
                updateSql.Append(" WHERE ");
                updateSql.Append(" ID=@ID ");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("exec('DELETE FROM TBReviewsReply");
                updateSql.Append(" WHERE ");
                updateSql.Append(" ID in ('+@ID+') ')");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.String, ids);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 根据酒店ID查询评价
        /// </summary>
        /// <returns></returns>
        public DataSet GetAppraisalByHotelID(int hotelid, int pageindex, out string total, out string score)
        {
            try
            {
                DbCommand ldcDBCommand = base.GetStoredProcCommand("up_QueryAppraisalByHotelID");
                //绑定SQL参数
                SetParameter(ldcDBCommand, "@HOTEL_ID", DbType.String, hotelid);
                SetParameter(ldcDBCommand, "@PAGE_INDEX", DbType.Int32, pageindex);
                SetParameter(ldcDBCommand, "@PAGE_SIZE", DbType.Int32, 3);
                SetParameter(ldcDBCommand, "@TOTAL", DbType.Int32, 0, ParameterDirection.Output);
                SetParameter(ldcDBCommand, "@SCORE", DbType.Double, 0, ParameterDirection.Output);

                DataSet ds = base.GetDataSet(ldcDBCommand);
                total = ldcDBCommand.Parameters["@TOTAL"].Value.ToString();
                score = ldcDBCommand.Parameters["@SCORE"].Value.ToString();
                return ds;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 回复评价
        /// </summary>
        /// <param name="appaisalID"></param>
        /// <param name="content"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public int ReplyAppraisal(int appaisalID, string content, int userID)
        {
            int iTmp = -1;
            try
            {
                DbCommand ldcDBCommand = base.GetStoredProcCommand("up_ReplyAppraisal");
                SetParameter(ldcDBCommand, "@UserID", DbType.Int32, userID);
                SetParameter(ldcDBCommand, "@AppaisalID", DbType.Int32, appaisalID);
                SetParameter(ldcDBCommand, "@Content", DbType.String, content);
                SetParameter(ldcDBCommand, "@Result", DbType.Int32, iTmp, ParameterDirection.Output);
                base.ExecuteNonQuery(ldcDBCommand);
                iTmp = Convert.ToInt32(ldcDBCommand.Parameters["@Result"].Value.ToString());
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        public ReviewsReplyModel GetReviewsReplyByReviewsId(int iReviewsId, int iUserId)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("select * from TBReviewsReply where UserID=@UserID and ReviewsID=@ReviewsID");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@UserID", DbType.Int32, iUserId);
                base.SetParameter(ldcDBCommand, "@ReviewsID", DbType.Int32, iReviewsId);
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                if (dt.Rows.Count > 0)
                    return ModelConvertHelper<ReviewsReplyModel>.ConvertToModelList(dt)[0];
                else return null;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
    }
}

